package com.jf.hb.ui.interfaces;

import com.jf.hb.HB;
import com.jf.hb.ui.JFHandler;
import org.apache.commons.codec.digest.DigestUtils;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-08
 * Time:3:48
 */
public class Register extends Interface {
    private String username;
    private String password;
    private String confirmPassword;

    private final Font font1;//头
    private final Font font2;//体
    private final Font font3;//警告


    private final Box vBox;

    private final ImageIcon bk;

    private final Pattern pattern;

    public Register() {
        username = HB.username;
        password = HB.password;
        confirmPassword = "";
        font1 = new Font("华文行楷", Font.PLAIN, 28);
        font2 = new Font("微软雅黑", Font.PLAIN, 20);
        font3 = new Font("微软雅黑", Font.PLAIN, 14);
        vBox = Box.createVerticalBox();
        bk = new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/login.jpg")));
        pattern = Pattern.compile("^\\w{6,12}$");
        init();
    }

    public void init() {
        initTitle();
        initUsernameField();
        initPasswordField();
        initButton();

        this.add(vBox);
    }

    private void initTitle() {
        Box titleBox = Box.createHorizontalBox();
        JLabel title = new JLabel("注册用户", JLabel.CENTER);
        title.setForeground(Color.BLACK);
        title.setFont(new Font("微软雅黑", Font.BOLD, 42));
        vBox.add(Box.createVerticalStrut(70));
        titleBox.add(title);
        vBox.add(titleBox);
    }

    private void initUsernameField() {
        Box usernameBox = Box.createHorizontalBox();
        Box nameWarnBox = Box.createHorizontalBox();

        JLabel usernameJLabel = new JLabel("用户名:");
        usernameJLabel.setFont(font1);
        usernameJLabel.setForeground(Color.BLACK);
        JTextField usernameField = new JTextField(username);
        usernameField.setColumns(8);
        usernameField.setFont(font2);
        usernameField.setForeground(Color.BLACK);

        JLabel nameWarnJLabel = new JLabel("用户名不得为空");
        nameWarnJLabel.setFont(font3);
        nameWarnJLabel.setForeground(Color.red);

        usernameField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                if (usernameField.getText().length() > 10) {
                    new Thread(() -> {
                        usernameField.setText(username);
                        usernameField.setCaretPosition(usernameField.getText().length());
                    }).start();
                } else {
                    username = usernameField.getText();
                    nameWarnBox.setVisible(false);
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if (usernameField.getText().isEmpty()) {
                    nameWarnBox.setVisible(true);
                } else {
                    username = usernameField.getText();
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                if (usernameField.getText().isEmpty()) {
                    nameWarnBox.setVisible(true);
                } else {
                    username = usernameField.getText();
                }
            }
        });

        usernameField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                usernameField.setCaretPosition(usernameField.getText().length());
                nameWarnBox.setVisible(usernameField.getText().isEmpty());
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        vBox.add(Box.createVerticalStrut(50));
        usernameBox.add(usernameJLabel);
        usernameBox.add(Box.createHorizontalStrut(33));
        usernameBox.add(usernameField);
        vBox.add(usernameBox);
        nameWarnBox.add(Box.createHorizontalStrut(70));
        nameWarnBox.add(nameWarnJLabel);
        vBox.add(nameWarnBox);
    }

    private void initPasswordField() {
        Box passwordBox = Box.createHorizontalBox();
        Box confirmPasswordBox = Box.createHorizontalBox();

        Box passwordWarnBox = Box.createHorizontalBox();
        Box confirmPasswordWarnBox = Box.createHorizontalBox();
        confirmPasswordWarnBox.setVisible(false);

        JLabel passwordJLabel = new JLabel("新密码:");
        passwordJLabel.setFont(font1);
        passwordJLabel.setForeground(Color.BLACK);
        JPasswordField passwordField = new JPasswordField();
        passwordField.setFont(font2);
        passwordField.setForeground(Color.BLACK);
        passwordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                passwordField.setCaretPosition(passwordField.getPassword().length);
                passwordWarnBox.setVisible(!pattern.matcher(password).find());
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        passwordField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                password = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(!pattern.matcher(password).find());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                password = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(!pattern.matcher(password).find());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                password = new String(passwordField.getPassword());
                passwordWarnBox.setVisible(!pattern.matcher(password).find());
            }
        });

        JLabel passwordWarnJLabel = new JLabel("密码为6-12位英文/数字");
        passwordWarnJLabel.setFont(font3);

        JLabel confirmPasswordJLabel = new JLabel("确认密码:");
        confirmPasswordJLabel.setFont(font1);
        confirmPasswordJLabel.setForeground(Color.BLACK);
        JPasswordField confirmPasswordField = new JPasswordField();
        confirmPasswordField.setFont(font2);
        confirmPasswordField.setForeground(Color.BLACK);
        confirmPasswordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                confirmPasswordField.setCaretPosition(confirmPasswordField.getPassword().length);
                confirmPasswordWarnBox.setVisible(!confirmPassword.equals(password));
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        confirmPasswordField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                confirmPassword = new String(confirmPasswordField.getPassword());
                confirmPasswordWarnBox.setVisible(!confirmPassword.equals(password));
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                confirmPassword = new String(confirmPasswordField.getPassword());
                confirmPasswordWarnBox.setVisible(!confirmPassword.equals(password));
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                confirmPassword = new String(confirmPasswordField.getPassword());
                confirmPasswordWarnBox.setVisible(!confirmPassword.equals(password));
            }
        });

        JLabel confirmPasswordWarnJLabel = new JLabel("两次密码不一致");
        confirmPasswordWarnJLabel.setFont(font3);
        confirmPasswordWarnJLabel.setForeground(Color.red);

        passwordBox.add(passwordJLabel);
        passwordBox.add(Box.createHorizontalStrut(33));
        passwordBox.add(passwordField);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(passwordBox);

        passwordWarnBox.add(Box.createHorizontalStrut(120));
        passwordWarnBox.add(passwordWarnJLabel);
        vBox.add(passwordWarnBox);

        confirmPasswordBox.add(confirmPasswordJLabel);
        confirmPasswordBox.add(Box.createHorizontalStrut(5));
        confirmPasswordBox.add(confirmPasswordField);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(confirmPasswordBox);

        confirmPasswordWarnBox.add(Box.createHorizontalStrut(70));
        confirmPasswordWarnBox.add(confirmPasswordWarnJLabel);
        vBox.add(confirmPasswordWarnBox);
    }

    private void initButton() {
        Box buttonBox = Box.createHorizontalBox();

        JButton registerButton = new JButton("注册");
        registerButton.setFont(font2);
        registerButton.addActionListener(e -> {
            if (password.equals(confirmPassword) && !password.isEmpty()) {
                postInformation();
                JFHandler.switchSignal = JFHandler.IF.REGISTER;
            }
        });

        buttonBox.add(registerButton);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(buttonBox);
    }

    private void postInformation() {
        password = DigestUtils.md5Hex(password);
        HB.setUserInformation(username, password);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bk.getImage(), 0, 0, getWidth(), getHeight(), null);
    }

    @Override
    public void run() {
    }
}
